﻿namespace SKeeper.Persistence.Facade
{
    using Domain.Companies;
    using Domain.Invoices;
    using Microsoft.AspNet.Identity.EntityFramework;
    using SKeeper.Domain.Items;
    using SKeeper.Domain.Users;
    using SKeeper.Domain.Warehouse;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity;
    using System.Data.Entity.ModelConfiguration.Conventions;
    public class DomainModelFacade : IdentityDbContext<User>
    {
        public DomainModelFacade() 
            : base("DefaultConnection")   // specify here conn-string entry if using SQL Server
        {
            Companies = base.Set<Company>();
            Warehouses = base.Set<Warehouse>();
            StoredItems = base.Set<StoredItem>();
            Items = base.Set<Item>();
            Invoices = base.Set<Invoice>();
            StoredItemMovements = base.Set<StoredItemMovement>();
        }

        public DbSet<Item> Items { get; set; }

        public DbSet<StoredItem> StoredItems { get; set; }

        public DbSet<Warehouse> Warehouses { get; set; }

        public DbSet<Company> Companies { get; set; }

        public DbSet<Invoice> Invoices { get; set; }

        public DbSet<StoredItemMovement> StoredItemMovements { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

           // modelBuilder.Entity<Company>()
           //     .Property(x => x.Id)
           //     .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

          //  modelBuilder.Entity<StoredItem>()
          //     .HasRequired(f => f.Item)
          //     //.WithOptional()
          //  //   .WithRequiredPrincipal()
          //   //  .WithMany()
          //     .WithRequiredDependent()
          //     .WillCascadeOnDelete(false);

           modelBuilder.Entity<StoredItem>()
              .Property(f => f.Id)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            base.OnModelCreating(modelBuilder);
        }

        public static DomainModelFacade Create()
        {
            return new DomainModelFacade();
        }

    }
}
